package com.shanghaionstar.discover;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.DiscoverActivity;
import com.shanghaionstar.discover.iview.IWifiHomeView;
import com.shanghaionstar.discover.model.WifiInfoModel;
import com.shanghaionstar.discover.presenter.WifiHomePresenter;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.report.bean.ReportReqParam;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.CommonInputLayout;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/15.
 */
public class WifiDetailFragment extends BaseFragment implements IWifiHomeView {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private CommonInputLayout et_ssid, et_pwd;
    @FindViewById
    private CommonProgressButton btn_submit;

    private String ssid_profix,ssid_suffix;

    private WifiHomePresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.discover_wifi_detail;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            btnBackClick(top_layout);
            presenter = new WifiHomePresenter(((DiscoverActivity) getActivity()).getScheduler());
            presenter.setView(this);
            presenter.registerRxBus();

            if (getArguments() != null) {
                String ssid = getArguments().getString("ssid");
                et_ssid.setText(ssid);
                et_pwd.setText(getArguments().getString("password"));
                if(ApplicationUtil.getInstance().isCurrentInfo3Vehicle()) {
                    if(ssid.length() > 10){
                        ssid_profix = ssid.substring(0,10);
                        ssid_suffix = ssid.substring(10);
                        et_ssid.setHintText(R.string.option_edit_wifi_name_tip);
                        et_ssid.getTv_preffix().setText(ssid_profix);
                        et_ssid.getTv_preffix().setTextColor(ContextCompat.getColor(baseActivity,R.color.fontColorGray));
                        et_ssid.setText(ssid_suffix);
                    }
                }
                if (getArguments().getBoolean("isUpdateSSID", true)) {
                    et_pwd.setVisibility(View.GONE);
                    et_ssid.setVisibility(View.VISIBLE);
                    top_layout.setTopTitle(getString(R.string.option_edit_vehicle_hot));
                } else {
                    et_pwd.setVisibility(View.VISIBLE);
                    et_ssid.setVisibility(View.GONE);
                    top_layout.setTopTitle(getString(R.string.option_edit_password));
                }
            }

            btn_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (et_pwd.getVisibility() == View.VISIBLE) {
                        BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.WIFIsavepassword.getStringValue());
                    } else if (et_ssid.getVisibility() == View.VISIBLE) {
                        BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.WIFIsaveSSID.getStringValue());
                    }
                    String ssid = et_ssid.getText();
//                    if (!TextUtils.isEmpty(ssid_profix))
//                        ssid = ssid_profix + ssid;
                    presenter.saveHotSpotInfo(getFragmentManager(), ssid , et_pwd.getText(), RemoteCommandsType.COMMAND_SETHOTSPOTINFO);
                }
            });
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        et_ssid.setEnabled(!isLoading);
        et_pwd.setEnabled(!isLoading);
        btn_submit.showLoading(isLoading);
    }

    @Override
    public void onResponseError(OnStarException e) {
        BusinessUtil.sendReportByRequestParams(onActivity(), FunctionNameFindId.WIFIsavepassword.getStringValue(), ReportReqParam.getWIFISetReqParm(false));
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
    }

    @Override
    public void onReponseSuccess(WifiInfoModel object) {
        BusinessUtil.sendReportByRequestParams(onActivity(), FunctionNameFindId.WIFIsavepassword.getStringValue(), ReportReqParam.getWIFISetReqParm(true));
        new ToastView(onActivity()).showToast(getString(R.string.wifi_setinfo_success));
        if(getActivity() instanceof  DiscoverActivity){
            DiscoverActivity mActivity = (DiscoverActivity) getActivity();
            if(getArguments().getBoolean("isUpdateSSID", true)){
                if (!TextUtils.isEmpty(ssid_profix))
                    mActivity.setSsid(ssid_profix + et_ssid.getText());
                else
                    mActivity.setSsid(et_ssid.getText());
            }else{
                mActivity.setWifiPassword(et_pwd.getText());
            }
        }
        back();
    }

    @Override
    public void verifyResult(int errorResId, boolean isPassword) {
        if (isPassword) {
            et_pwd.setCheckHintText(errorResId);
        } else {
            et_ssid.setCheckHintText(errorResId);
        }
    }

    @Override
    public void onBack() {
        back();
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}
